package ca.cbc.security;

import java.util.*;
import java.sql.*;
import javax.sql.*;
import javax.naming.*;

import org.apache.log4j.Logger;
import ca.cbc.util.*;

public class AuthDBConnect {

  private static Logger log = Logger.getLogger(AuthDBConnect.class);
  private StackTraceToString stts = new StackTraceToString();

  private Connection connection;

  public AuthDBConnect () throws SQLException {

	connection = null;

	String DATASOURCE_CONTEXT = "java:/comp/env/jdbc/usermanager_dml";

	try {
		Context initCtx = new InitialContext();
		DataSource datasource = (DataSource)initCtx.lookup(DATASOURCE_CONTEXT);

		if(datasource != null) {
			this.connection = datasource.getConnection();
		} else {
			log.info("Failed to acquire datasource connection");
		}

	} catch (NamingException e) {
		log.info("Could not locate JNDI resource: " + e);
		log.info(stts.getStackTrace(e));
	} 

  }

  public Connection getConnection() {
	return connection;
  }
}
